package com.bancomer.pig.functions;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.RecordReader;
import org.apache.hadoop.mapreduce.TaskAttemptContext;

public class MockRecordReader extends RecordReader{
	
	 private BufferedReader reader;
	 private long key;
	 private boolean linesLeft;
	 
	 public MockRecordReader(String fileLocation) throws FileNotFoundException {
		 
		  reader  = new BufferedReader(new FileReader(fileLocation));
		  key = 0;
		  linesLeft = true;
		  
		  }

	@Override
	public void close() throws IOException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Object getCurrentKey() throws IOException, InterruptedException {
		// TODO Auto-generated method stub
		 return key;
	}

	@Override
	public Object getCurrentValue() throws IOException, InterruptedException {
		// TODO Auto-generated method stub
		String line = reader.readLine();
		  
		  if(line != null) {
		   key++;
		  } else {
		   linesLeft = false;
		  }
		   return new Text(line);
	}

	@Override
	public float getProgress() throws IOException, InterruptedException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void initialize(InputSplit arg0, TaskAttemptContext arg1)
			throws IOException, InterruptedException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean nextKeyValue() throws IOException, InterruptedException {
		// TODO Auto-generated method stub
		return linesLeft;
	}

}
